package com.xintonghua.bussiness.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.SimpleBaseAdapter;
import com.xintonghua.bussiness.bean.CartBean;
import com.xintonghua.bussiness.event.DeleteEventBus;
import com.xintonghua.bussiness.event.OrderEventBus;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/31.
 * 包名：com.xintonghua.bussiness.adapter
 */

public class CartAdapter extends SimpleBaseAdapter<CartBean> {


    public CartAdapter(List<CartBean> list, Context context) {
        super(list, context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ShopLeftViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_card, null);
            holder = new ShopLeftViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ShopLeftViewHolder) convertView.getTag();

        }
        final CartBean bean = mList.get(position);
        GlideUtils.load(context, holder.ivShopPic, bean.getDetail_img());
        holder.ivSelcet.setImageResource(bean.isCheck() ? R.drawable.cb_cart_check : R.drawable.cb_cart_uncheck);
        holder.tvShopName.setText(bean.getName());
        holder.tvShopPrice.setText(MyUtils.getMoney(context, bean.getMoney()));
        holder.tvShopCount.setText(bean.getCount()+"");
        holder.ivSelcet.setOnClickListener(new onListener(bean, position));
        holder.ivReduce.setOnClickListener(new onListener(bean, position));
        holder.ivAdd.setOnClickListener(new onListener(bean, position));
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DeleteEventBus(bean));
            }
        });
        holder.tvShopCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    return;
                }
                if (Integer.valueOf(s.toString())==0){
                    return;
                }
                bean.setCount(Integer.valueOf(s.toString()));
                EventBus.getDefault().post(new OrderEventBus(mList,bean));
                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    private class onListener implements View.OnClickListener {
        private CartBean bean;
        int position;

        public onListener(CartBean bean, int position) {
            this.position = position;
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_selcet:
                    bean.setCheck(!bean.isCheck());
                    EventBus.getDefault().post(new OrderEventBus(mList));
                    notifyDataSetChanged();
                    break;
                case R.id.iv_reduce:
                    if (bean.getCount() <= 1) {
                        return;
                    }
                    bean.setCount(bean.getCount() - 1);
                    EventBus.getDefault().post(new OrderEventBus(mList,bean));
                    notifyDataSetChanged();
                    break;
                case R.id.iv_add:
                    bean.setCount(bean.getCount() + 1);
                    EventBus.getDefault().post(new OrderEventBus(mList,bean));
                    notifyDataSetChanged();
                    break;
            }
        }
    }

    static class ShopLeftViewHolder {
        @BindView(R.id.iv_selcet)
        ImageView ivSelcet;
        @BindView(R.id.iv_shop_pic)
        ImageView ivShopPic;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_shop_price)
        TextView tvShopPrice;
        @BindView(R.id.iv_reduce)
        ImageView ivReduce;
        @BindView(R.id.tv_shop_count)
        EditText tvShopCount;
        @BindView(R.id.iv_add)
        ImageView ivAdd;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        @BindView(R.id.swipe)
        SwipeMenuLayout swipe;

        public ShopLeftViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
